
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
public class bjzfdb {
    public static String fgf ="^^^^";
    public static boolean label =true;
    public static String v1="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\n" +
//            "Accept-Encoding: gzip, deflate, br\n" +
            "Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7\n" +
            "Cache-Control: max-age=0\n" +
            "Connection: keep-alive\n" +
            "Cookie: gr_user_id=93209653-1256-4c7d-8b0c-7aafae12a286; __yadk_uid=nbNWrN078AHAhrfFlnlPf4EN0ykbKUdQ; douban-fav-remind=1; _pk_id.100001.8cb4=2e40244eab43eb95.1684112944.; viewed=\"2285076_26832468_5331113_25984046_3798470_26363531\"; bid=o2UQsOscgfI; __utmz=30149280.1705845604.70.48.utmcsr=cn.bing.com|utmccn=(referral)|utmcmd=referral|utmcct=/; ll=\"108288\"; dbcl2=\"224418402:0r4VxFkyVx0\"; push_noty_num=0; push_doumail_num=0; __utmv=30149280.22441; ck=O6BJ; __utmc=30149280; _pk_ref.100001.8cb4=%5B%22%22%2C%22%22%2C1707994625%2C%22https%3A%2F%2Fcn.bing.com%2F%22%5D; _pk_ses.100001.8cb4=1; __utma=30149280.2098408270.1666419553.1707989972.1707994625.75; __utmt=1; frodotk_db=\"b9e6711cff938ab92db35356febaa02b\"; __utmb=30149280.47.5.1707994697824\n" +
//            "Cookie: gr_user_id=93209653-1256-4c7d-8b0c-7aafae12a286; __yadk_uid=nbNWrN078AHAhrfFlnlPf4EN0ykbKUdQ; douban-fav-remind=1; _pk_id.100001.8cb4=2e40244eab43eb95.1684112944.; viewed=2285076_26832468_5331113_25984046_3798470_26363531; bid=o2UQsOscgfI; __utmz=30149280.1705845604.70.48.utmcsr=cn.bing.com|utmccn=(referral)|utmcmd=referral|utmcct=/; ll=108288; dbcl2=224418402:0r4VxFkyVx0; push_noty_num=0; push_doumail_num=0; __utmv=30149280.22441; ck=O6BJ; __utmc=30149280; _pk_ref.100001.8cb4=%5B%22%22%2C%22%22%2C1707994625%2C%22https%3A%2F%2Fcn.bing.com%2F%22%5D; _pk_ses.100001.8cb4=1; __utma=30149280.2098408270.1666419553.1707989972.1707994625.75; __utmt=1; frodotk_db=b9e6711cff938ab92db35356febaa02b; __utmb=30149280.47.5.1707994697824\n" +
            "DNT: 1\n" +
            "Host: www.douban.com\n" +
            "Sec-Fetch-Dest: document\n" +
            "Sec-Fetch-Mode: navigate\n" +
            "Sec-Fetch-Site: none\n" +
            "Sec-Fetch-User: ?1\n" +
            "Upgrade-Insecure-Requests: 1\n" +
            "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4270.0 Safari/537.36";
    public static ArrayList<String> all =  new ArrayList<>();
    public static HashMap<String,String> map1 = new HashMap<>();
    static {
        for(String vss:v1.split("\n"))  map1.put(vss.split(": ")[0].replace(":",""),vss.split(": ")[1]);
    }
    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            String filePath = "c:\\zf\\zf"+ LocalDateTime.now().toString().replace(":","-")+".txt";
            try  {
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8));
                for(String v:all)  bw.write(v+"\r\n");
                bw.flush();
                bw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }));
//        getResult();
        testJob();
    }

    public static void getResult(){
        getContent("https://www.douban.com/group/zhufang/discussion?start=0&type=new",all);
        getContent("https://www.douban.com/group/beijingzufang/discussion?start=0&type=new",all);
        if(all.size()>=600){
            String filePath = "c:\\zf\\zf"+ LocalDateTime.now().toString().replace(":","-")+".txt";

            try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8))) {
//            try (BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\zf\\zf"+ LocalDateTime.now().toString().replace(":","-")+".txt"))) {
               for(String v:all)  bw.write(v+"\r\n");
            } catch (Exception e) {
                e.printStackTrace();
            }
            all.clear();
        }
        System.out.println(LocalDateTime.now().toString()+all.size());
    }
    public static void getContent(String add,ArrayList<String> vs){
        try {

            Thread.sleep(3000);
            Document document = null;
            document = Jsoup.connect(add).headers(map1).get();
            Elements tables = document.select("table.olt");
            Elements rows = tables.select("tr");
            rows.stream().skip(1).forEach(row -> {
                String title = "";
                String url = "";
                String time = "";
                try{
                    Elements tmp = row.select("td").first().select("a");
                    title = tmp.attr("title");
                    url = tmp.attr("href");
                    time = row.select("td").last().text();

                }catch (Exception e1){e1.printStackTrace();}

                for(String name:getSN()){
                    if(title.contains(name)){
                        vs.add(name+fgf+title+fgf+url+ LocalDate.now().getYear()+"-"+time);
                        label=false;
                        break;
                    }
                }
                if(label) vs.add("无"+fgf+title+fgf+url+ LocalDate.now().getYear()+"-"+time);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static ArrayList<String> getSN()  {
        ArrayList<String> all = new ArrayList();
        String content = "{\n" +
                "  \"1号线\": [\"苹果园\", \"古城\", \"八角游乐园\", \"八宝山\", \"玉泉路\", \"五棵松\", \"万寿路\", \"公主坟\", \"军事博物馆\", \"木樨地\", \"南礼士路\", \"复兴门\", \"西单\", \"天安门西\", \"天安门东\", \"王府井\", \"东单\", \"建国门\", \"永安里\", \"国贸\", \"大望路\", \"四惠\", \"四惠东\"],\n" +
                "  \"2号线\": [\"西直门\", \"车公庄\", \"阜成门\", \"复兴门\", \"长椿街\", \"宣武门\", \"和平门\", \"前门\", \"崇文门\", \"北京站\", \"建国门\", \"朝阳门\", \"东四十条\", \"东直门\", \"雍和宫\", \"安定门\", \"鼓楼大街\", \"积水潭\", \"西直门\"],\n" +
                "  \"4号线\": [\"北京南站\", \"陶然亭\", \"菜市口\", \"宣武门\", \"西单\", \"灵境胡同\", \"西四\", \"平安里\", \"新街口\", \"西直门\", \"动物园\", \"国家图书馆\", \"魏公村\", \"人民大学\", \"海淀黄庄\", \"中关村\", \"北京大学东门\", \"圆明园\", \"西苑\", \"北宫门\", \"安河桥北\"],\n" +
                "  \"5号线\": [\"宋家庄\", \"刘家窑\", \"蒲黄榆\", \"天坛东门\", \"磁器口\", \"崇文门\", \"东单\", \"灯市口\", \"东四\", \"张自忠路\", \"北新桥\", \"雍和宫\", \"和平里北街\", \"和平西桥\", \"惠新西街南口\", \"惠新西街北口\", \"大屯路东\", \"北苑路北\", \"立水桥南\", \"立水桥\", \"天通苑南\", \"天通苑\", \"天通苑北\"],\n" +
                "  \"6号线\": [\"金安桥\", \"苹果园\", \"杨庄\", \"西黄村\", \"廖公庄\", \"田村\", \"海淀五路居\", \"慈寿寺\", \"花园桥\", \"白石桥南\", \"车公庄西\", \"车公庄\", \"平安里\", \"北海北\", \"南锣鼓巷\", \"东四\", \"朝阳门\", \"东大桥\", \"呼家楼\", \"金台路\", \"十里堡\", \"青年路\", \"褡裢坡\", \"黄渠\", \"常营\", \"草房\", \"物资学院路\", \"通州北关\", \"通运门\", \"北运河西\", \"北运河东\", \"郝家府\", \"东夏园\", \"潞城\"],\n" +
                "  \"7号线\": [\"北京西站\", \"湾子\", \"达官营\", \"广安门内\", \"菜市口\", \"虎坊桥\", \"珠市口\", \"桥湾\", \"磁器口\", \"广渠门内\", \"广渠门外\", \"双井\", \"九龙山\", \"大郊亭\", \"百子湾\", \"化工\", \"南楼梓庄\", \"欢乐谷景区\", \"垡头\", \"双合\", \"焦化厂\"],\n" +
                "  \"8号线\": [\"朱辛庄\", \"育知路\", \"平西府\", \"回龙观东大街\", \"霍营\", \"育新\", \"西小口\", \"永泰庄\", \"林萃桥\", \"森林公园南门\", \"奥林匹克公园\", \"奥体中心\", \"北土城\", \"安华桥\", \"安德里北街\", \"鼓楼大街\", \"什刹海\", \"南锣鼓巷\", \"中国美术馆\", \"宋庄\", \"玉泉路\", \"五棵松\", \"万寿路\", \"苹果园\"],\n" +
                "  \"9号线\": [\"国家图书馆\", \"白石桥南\", \"白堆子\", \"军事博物馆\", \"北京西站\", \"六里桥东\", \"六里桥\", \"七里庄\", \"丰台东大街\", \"丰台南路\", \"科怡路\", \"丰台科技园\", \"郭公庄\"],\n" +
                "  \"10号线\": [\"巴沟\", \"苏州街\", \"海淀黄庄\", \"知春里\", \"知春路\", \"西土城\", \"牡丹园\", \"健德门\", \"北土城\", \"安贞门\", \"惠新西街南口\", \"惠新西街北口\", \"芍药居\", \"太阳宫\", \"三元桥\", \"亮马桥\", \"农业展览馆\", \"团结湖\", \"呼家楼\", \"金台夕照\", \"国贸\", \"双井\", \"劲松\", \"潘家园\", \"十里河\", \"分钟寺\", \"成寿寺\", \"宋家庄\"],\n" +
                "  \"13号线\": [\"西直门\", \"大钟寺\", \"知春路\", \"五道口\", \"上地\", \"西二旗\", \"龙泽\", \"回龙观\", \"霍营\", \"立水桥\", \"北苑\", \"望京西\", \"芍药居\", \"光熙门\", \"柳芳\", \"东直门\"],\n" +
                "  \"14号线\": [\"张郭庄\", \"园博园\", \"大瓦窑\", \"郭庄子\", \"大井\", \"七里庄\", \"西局\", \"北京南站\", \"永定门外\", \"景泰\", \"蒲黄榆\", \"方庄\", \"十里河\", \"北工大西门\", \"九龙山\", \"大望路\", \"金台路\", \"朝阳公园\", \"枣营\", \"东风北桥\", \"将台\", \"望京南\", \"阜通\", \"望京\", \"东湖渠\", \"来广营\", \"善各庄\"],\n" +
                "  \"15号线\": [\"清华东路西口\", \"六道口\", \"北沙坛\", \"奥林匹克公园\", \"安立路\", \"大屯路东\", \"关庄\", \"望京西\", \"望京\", \"崔各庄\", \"马泉营\", \"孙河\", \"国展\", \"花梨坎\", \"后沙峪\", \"南法信\", \"石门\", \"顺义\", \"俸伯\"],\n" +
                "  \"16号线\": [\"西苑\", \"农大南路\", \"马连洼\", \"西北旺\", \"永丰南\", \"永丰\", \"屯佃\", \"稻香湖路\", \"温阳路\", \"北安河\"],\n" +
                "  \"八通线\": [\"四惠\", \"四惠东\", \"高碑店\", \"传媒大学\", \"双桥\", \"管庄\", \"八里桥\", \"通州北苑\", \"果园\", \"九棵树\", \"梨园\", \"临河里\", \"土桥\"],\n" +
                "  \"昌平线\": [\"昌平西山口\", \"十三陵景区\", \"昌平\", \"昌平北山口\", \"南邵\", \"沙河高教园\", \"沙河\", \"巩华城\", \"朱辛庄\", \"生命科学园\", \"西二旗\"],\n" +
                "  \"亦庄线\": [\"宋家庄\", \"肖村\", \"小红门\", \"旧宫\", \"亦庄桥\", \"亦庄文化园\", \"万源街\", \"荣京东街\", \"荣昌东街\", \"同济南路\", \"经海路\", \"次渠南\", \"次渠\"],\n" +
                "  \"房山线\": [\"郭公庄\", \"大葆台\", \"稻田\", \"长阳\", \"篱笆房\", \"广阳城\", \"良乡大学城北\", \"良乡大学城\", \"良乡大学城西\", \"良乡南关\", \"苏庄\", \"阎村东\"],\n" +
                "  \"首都机场线\": [\"东直门\", \"三元桥\", \"3号航站楼\", \"2号航站楼\"],\n" +
                "  \"燕房线\": [\"阎村东\", \"紫草坞\", \"阎村\", \"星城\", \"大石河东\", \"马各庄\", \"饶乐府\", \"房山城关\", \"燕山\"]\n" +
                "}\n";
        try {
            JsonNode d = new ObjectMapper().readValue(content, JsonNode.class);
            d.iterator().forEachRemaining(s->{
                for (JsonNode element : s) {
                    all.add(element.asText());
                }
            });
        }catch (Exception e){}
        return all;
    }

    public static void testJob(){
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();

        try{
            Scheduler scheduler = schedulerFactory.getScheduler();

            // 触发器描述器
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("myTrigger", "group1")
                    .startNow()
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMinutes(45).repeatForever())
                    .build();

            // 任务描述器
            JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
                    .withIdentity("myJob", "group1")
                    .build();

            // 将任务与触发器绑定到调度器上
            scheduler.scheduleJob(jobDetail, trigger);

            // 启动调度器
            scheduler.start();

            // 程序等待10秒钟
//            Thread.sleep(10000L);

            // 关闭调度器
//            scheduler.shutdown();
        }catch (Exception e){}
    }
}
